/**
 * @Title: ItemTypeController
 * @Package cn.com.yiqi.wsc.portal.controller.item
 * @Description: TODO
 * Copyright: Copyright (c) 2016
 * Company: 成都壹柒互动科技有限公司
 * @author 会员系统组-胥钰淋
 * @date 2017/2/22 9:51
 * @version V1.0
 */
package cn.com.yiqi.wsc.portal.controller.item;

import cn.com.yiqi.wsc.common.Result;
import cn.com.yiqi.wsc.common.ResultStatusEnum;
import cn.com.yiqi.wsc.dao.item.criteria.ItemTypeCriteria;
import cn.com.yiqi.wsc.entity.item.ItemTree;
import cn.com.yiqi.wsc.entity.item.ItemType;
import cn.com.yiqi.wsc.entity.item.ItemTypePropertyAdminDTO;
import cn.com.yiqi.wsc.portal.controller.BaseController;
import cn.com.yiqi.wsc.service.item.PortalItemService;
import cn.com.yiqi.wsc.service.user.UserService;
import cn.com.yiqi.wsc.util.SeqUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author 会员系统组-胥钰淋
 * @ClassName: ItemTypeController
 * @Description: TODO
 * @date 2017/2/22 9:51
 */
@Controller
@RequestMapping("/item/type")
public class ItemTypeController extends BaseController {


    @Autowired
    private PortalItemService portalItemTypeService;

    @RequestMapping("/index")
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView("item/type/index");
        this.getUserData(mv);
        return mv;
    }

    @RequestMapping(value = "/tree", method = RequestMethod.GET)
    @ResponseBody
    public Result tree() {

        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());

        Result<List<ItemTree>> listResult = portalItemTypeService.tree();

        result.putData("options", listResult.getReturnValue());

        return result;
    }


    /**
     * 添加接口
     *
     * @param itemType
     * @return
     */

    @RequestMapping("/add")
    @ResponseBody
    public Result insert(@RequestBody ItemType itemType) {

        Result result = new Result();

        itemType.setInsertUserId(userService.getCurrentUserId());
        result = portalItemTypeService.insertType(itemType);

        return result;
    }

    /**
     * 修改接口
     *
     * @param itemType
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public Result update(@RequestBody ItemType itemType) {

        Result result = new Result();

        itemType.setUpdateUserId(userService.getCurrentUserId());
        result = portalItemTypeService.updateType(itemType);

        return result;
    }

    /**
     * 删除接口
     *
     * @param itemType
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Result delete(@RequestBody ItemType itemType) {

        Result result = new Result();

        result = portalItemTypeService.deleteType(itemType);

        return result;
    }


    /**
     * 获取商品类别根据Pid
     *
     * @param criteria
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Result getItemTypeByPid(@RequestBody ItemTypeCriteria criteria) {

        Result result = new Result();

        result = portalItemTypeService.getItemTypeByPid(criteria);

        return result;
    }


    /**
     * 获取属性列表
     *
     * @param criteria
     * @return
     */
    @RequestMapping("/property/list")
    @ResponseBody
    public Result getItemTypePropertyByTypeId(@RequestBody ItemTypeCriteria criteria) {

        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());

        /**
         * 根据typeId获取seq
         */
        String seq = portalItemTypeService.getSeqByTypeId(criteria.getTypeId());

        if (seq != null) {

            criteria.setTypeIds(SeqUtil.seq2List(seq + criteria.getTypeId() + "."));

            result = portalItemTypeService.getItemTypePropertyByItemTypeSeq(criteria);

            return result;
        }

//        result = portalItemTypeService.getItemTypePropertyByItemTypeId(criteria);

        return result;
    }

    /**
     * 属性添加
     *
     * @param itemTypePropertyAdminDTO
     * @return
     */
    @RequestMapping("/property/add")
    @ResponseBody
    public Result getItemTypePropertyAdd(@RequestBody ItemTypePropertyAdminDTO itemTypePropertyAdminDTO) {

        Result result = new Result();


        result = portalItemTypeService.propertyInsert(itemTypePropertyAdminDTO);

        return result;
    }


    /**
     * 属性修改
     *
     * @param itemTypePropertyAdminDTO
     * @return
     */
    @RequestMapping("/property/update")
    @ResponseBody
    public Result getItemTypePropertyUpdate(@RequestBody ItemTypePropertyAdminDTO itemTypePropertyAdminDTO) {

        Result result = new Result();


        result = portalItemTypeService.propertyUpdate(itemTypePropertyAdminDTO);

        return result;
    }

    /**
     * 属性删除
     *
     * @param itemTypePropertyAdminDTO
     * @return
     */
    @RequestMapping("/property/delete")
    @ResponseBody
    public Result getItemTypePropertyByDelete(@RequestBody ItemTypePropertyAdminDTO itemTypePropertyAdminDTO) {

        Result result = new Result();


        result = portalItemTypeService.propertyDelete(itemTypePropertyAdminDTO);

        return result;
    }


}
